<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="gtitle" style="margin-bottom: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>补丁特征库信息</div>

      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">补丁库版本:</a-col>
          <a-col :span="4">{{version}}</a-col>
          <a-col :span="2">最后一次升级时间:</a-col>
          <a-col :span="4">{{lastUpdateDate}}</a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px;"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>补丁库更新过滤</div>
      <div class="rowtxt">
        <a-row class='rowmb'>请选择需要更新过滤的类型、危险等级以及对应的操作系统范围</a-row>
        <a-row class='rowmb'>
          <a-col :span="2">补丁类型:</a-col>
          <a-col :span="22">
            <a-radio-group v-model="patchTypevalue" @change="changepatchtype()">
              <span class="rowspan" v-for="item in patchType">
                <a-radio :value="item.value">{{ item.label }}</a-radio>
              </span>
            </a-radio-group>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">危险等级:</a-col>
          <a-col :span="22">
            <a-radio-group v-model="level" @change="changelevel()">
              <span class="rowspan" v-for="item in patchLevel">
                <a-radio :value="item.value">{{ item.label }}</a-radio>
              </span>
            </a-radio-group>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">操作系统类型:</a-col>
          <a-col :span="22">
            <span class="rowspan">Windows系列</span>
            <a-checkbox-group v-model="Windowsvalue" @change="oschange()">
              <span class="rowspan" v-for="item in WindowType">
                 <a-checkbox :value="item.value">{{ item.label }}</a-checkbox>
              </span>
            </a-checkbox-group>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="22" :offset="2">
            <span class="rowspan">Centos系列</span>
            <a-checkbox-group v-model="Centosvalue" @change="oschange()">
              <span class="rowspan" v-for="item in CentosType">
                 <a-checkbox :value="item.value">{{ item.label }}</a-checkbox>
              </span>
            </a-checkbox-group>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="22" :offset="2">
            <span class="rowspan">Ubuntu系列</span>
            <a-checkbox-group v-model="Ubuntuvalue" @change="oschange()">
              <span class="rowspan" v-for="item in UbuntuType">
                 <a-checkbox :value="item.value">{{ item.label }}</a-checkbox>
              </span>
            </a-checkbox-group>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="22" :offset="2">
            <span class="rowspan">其他</span>
            <a-checkbox-group v-model="Othervalue" @change="oschange()">
              <span class="rowspan" v-for="item in OtherType">
                 <a-checkbox :value="item.value">{{ item.label }}</a-checkbox>
              </span>
            </a-checkbox-group>
          </a-col>
        </a-row>
      </div>


      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px;"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>补丁库更新方式</div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">更新方式:</a-col>
          <a-col :span="22" v-if="patchUpdateModeValue.length>0">
            <a-radio-group v-model="mode" @change="changemethod()">
                <a-row class='rowmb' v-for="item in patchUpdateModeValue">
                  <a-col :span="3"><a-radio :value="item.model" >{{item.model == 0?'互联网更新':(item.model == 1?'级联更新':'其他平台更新')}}:</a-radio></a-col>
                  <a-col :span="4"><a-input :placeholder="item.model == 0?'www.ybxin.com':'输入IP地址或域名'" v-model="item.value" @blur="changemethod()"></a-input></a-col>
                </a-row>
            </a-radio-group>
          </a-col>
        </a-row>

        <a-row class='rowmb'>
          <a-col :span="2">自动升级:</a-col>
          <a-col :span="22" >
            <a-switch checked-children="开" un-checked-children="关" v-model="autoConfig.enabled" @change="changeauto()"/>
          </a-col>
        </a-row>

        <a-row class='rowmb'>
          <a-col :span="22" :offset="2">
            <a-row class='rowmb'>
              <a-col :span="3">定时方式</a-col>
              <a-col :span="4">
                <a-select style="width: 100%;" v-model="autoConfig.model" @change="changeauto()">
                  <a-select-option value="3">每天一次</a-select-option>
                  <a-select-option value="2">每周一次</a-select-option>
                  <a-select-option value="1">每月一次</a-select-option>
                </a-select>
              </a-col>
            </a-row>
            <a-row class='rowmb' v-if="autoConfig.model == 2">
              <a-col :span="3">选择星期</a-col>
              <a-col :span="4">
                <a-select style="width: 100%;" v-model="autoConfig.dateOrweek" @change="changeauto()">
                  <a-select-option value="1">星期一</a-select-option>
                  <a-select-option value="2">星期二</a-select-option>
                  <a-select-option value="3">星期三</a-select-option>
                  <a-select-option value="4">星期四</a-select-option>
                  <a-select-option value="5">星期五</a-select-option>
                  <a-select-option value="6">星期六</a-select-option>
                  <a-select-option value="7">星期日</a-select-option>
                </a-select>
              </a-col>
            </a-row>
            <a-row class='rowmb' v-if="autoConfig.model == 1">
              <a-col :span="3">选择日期</a-col>
              <a-col :span="4">
                <a-select style="width: 100%;" v-model="autoConfig.dateOrweek" @change="changeauto()">
                  <a-select-option v-for=" i in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]" :value="i">
                    {{i}}</a-select-option>
                </a-select>
              </a-col>
            </a-row>
            <a-row class='rowmb'>
              <a-col :span="3">选择时间</a-col>
              <a-col :span="4">
                <a-time-picker style="width: 100%" format="HH:mm:ss" v-model="autoConfig.time" @change="changeauto()"/>
              </a-col>
            </a-row>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="3"> <a-button type="primary">在线检测补丁库</a-button></a-col>
          <a-col :span="4">当前无要升级的补丁:</a-col>
          <a-col :span="2"><a-button type="primary">立即升级</a-button></a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px;"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>补丁库离线更新</div>
      <a-row class='rowmb'>
        <a-button type="primary">获取离线上传下载工具</a-button>
      </a-row>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px;"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>补丁推送参数设置</div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">最大并发连接数:</a-col>
          <a-col :span="1" >
            <a-switch checked-children="开" un-checked-children="关" v-model="concurrentNumber.check" @change="savec1()"/>
          </a-col>
          <a-col :span="2" >
            <a-input placeholder="0" v-model="concurrentNumber.value" @blur="savec1()"></a-input>
          </a-col>
          <a-col :span="2"></a-col>
        </a-row>

        <a-row class='rowmb'>
          <a-col :span="2">最大并发流量:</a-col>
          <a-col :span="1" >
            <a-switch checked-children="开" un-checked-children="关" v-model="concurrentFlow.check" @change="savec2()"/>
          </a-col>
          <a-col :span="2" >
            <a-input placeholder="0" v-model="concurrentFlow.value" @blur="savec2()"></a-input>
          </a-col>
          <a-col :span="2" style="padding-left: 20px">Mbps</a-col>
        </a-row>

        <a-row class='rowmb'>
          <a-col :span="2">每连接最大流量:</a-col>
          <a-col :span="1" >
            <a-switch checked-children="开" un-checked-children="关" v-model="connectionFlow.check" @change="savec3()"/>
          </a-col>
          <a-col :span="2" >
            <a-input placeholder="0" v-model="connectionFlow.value" @blur="savec3()"></a-input>
          </a-col>
          <a-col :span="2" style="padding-left: 20px">Mbps</a-col>
        </a-row>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import {WindowType,UbuntuType,CentosType,OtherType,patchLevel,patchType} from '../../utils/constant'
import {getpatchupdateinfo,setpatchupdateinfo} from '@/api/patch'
import moment from 'moment';
export default {
  name: 'PatchUpdateManage',
  data() {
    return {
      patchType,
      patchLevel,
      WindowType,
      UbuntuType,
      CentosType,
      OtherType,
      version:'',
      lastUpdateDate:'',
      adaptationOs:{},
      patchTypevalue:'',
      level:'',
      Windowsvalue:[],
      Centosvalue:[],
      Ubuntuvalue:[],
      Othervalue:[],
      patchUpdateModeValue:[],
      mode:0,
      autoConfig:{
        time:''
      },

      concurrentNumber:{},
      connectionFlow:{},
      concurrentFlow:{}
    }
  },
  mounted() {
    this.$info(
      {
        title:'提示',
        content:'测试Lisence未授权！',
        onOk:()=> {
          this.$router.push({ path: '/overview/overview' })
        },
      }
    )
    this.getinfo()
  },
  methods:{
    changeauto(){
      setpatchupdateinfo({
        id:1,
        autoConfig:JSON.stringify({
          enabled:this.autoConfig.enabled?1:0,
          model:this.autoConfig.model,
          dateOrweek:this.autoConfig.dateOrweek,
          time:this.autoConfig.time
        })
      }).then(res=>{})
    },

    savec1(){
      setpatchupdateinfo({
        id:1,
        concurrentNumber:JSON.stringify({
          check:this.concurrentNumber.check?1:0,
          value:this.concurrentNumber.value
        })
      }).then(res=>{})
    },
    savec2(){
      setpatchupdateinfo({
        id:1,
        concurrentFlow:JSON.stringify({
          check:this.concurrentFlow.check?1:0,
          value:this.concurrentFlow.value
        })
      }).then(res=>{})
    },
    savec3(){
      setpatchupdateinfo({
        id:1,
        connectionFlow:JSON.stringify({
          check:this.connectionFlow.check?1:0,
          value:this.connectionFlow.value
        })
      }).then(res=>{})
    },
    changemethod(){
      for(let i in this.patchUpdateModeValue){
        if(this.patchUpdateModeValue[i].model == this.mode){
          this.patchUpdateModeValue[i].check = 1
        }else{
          this.patchUpdateModeValue[i].check = 0
        }
      }
      setpatchupdateinfo({
        id:1,
        patchUpdateModeValue:JSON.stringify(this.patchUpdateModeValue)
      }).then(res=>{})
    },
    getinfo(){
      getpatchupdateinfo().then(res=>{
        this.version = res.data.version
        this.lastUpdateDate =res.data.lastUpdateDate.replace('T',' ')
        this.adaptationOs = JSON.parse(res.data.adaptationOs)
        this.patchTypevalue = res.data.patchType.toString()
        this.level = res.data.level.toString()

        this.Windowsvalue = this.adaptationOs.Windows.split(',')
        this.Centosvalue = this.adaptationOs.Centos.split(',')
        this.Ubuntuvalue = this.adaptationOs.Ubuntu.split(',')
        this.Othervalue = this.adaptationOs.Other.split(',')

        this.patchUpdateModeValue = JSON.parse(res.data.patchUpdateModeValue)
        for(let i in this.patchUpdateModeValue){
          if(this.patchUpdateModeValue[i].check == '1') this.mode = this.patchUpdateModeValue[i].model
        }



        this.concurrentNumber = JSON.parse(res.data.concurrentNumber)
        this.concurrentNumber.check = this.concurrentNumber.check?true:false
        this.connectionFlow = JSON.parse(res.data.connectionFlow)
        this.connectionFlow.check= this.connectionFlow.check?true:false
        this.concurrentFlow = JSON.parse(res.data.concurrentFlow)
        this.concurrentFlow.check = this.concurrentFlow.check?true:false

        this.autoConfig = JSON.parse(res.data.autoConfig)
        if(this.autoConfig.enabled == 1){this.autoConfig.enabled = true}else{this.autoConfig.enabled = false}
        this.autoConfig.time = moment(this.autoConfig.time,'HH:mm:ss')
      })
    },
    changepatchtype(){
      setpatchupdateinfo({
        id:1,
        patchType:this.patchTypevalue
      }).then(res=>{})
    },
    changelevel(){
      setpatchupdateinfo({
        id:1,
        level:this.level
      }).then(res=>{})
    },
    oschange(){
      this.adaptationOs = {
        Windows:this.Windowsvalue.join(','),
        Centos:this.Centosvalue.join(','),
        Ubuntu:this.Ubuntuvalue.join(','),
        Other:this.Othervalue.join(','),
      }
      setpatchupdateinfo({
        id:1,
        adaptationOs:JSON.stringify(this.adaptationOs)
      }).then(res=>{})
    }
  }
}
</script>

<style lang="less"  scoped>

.gtitle{
  background-color: #e6f3fc;
  line-height: 40px;
  padding-left: 20px;
  color: #333;
  font-weight: bold;

  i{
    margin-right: 10px;
  }
}
.rowtxt,.ant-radio-group{
  line-height: 32px;

}
.ant-radio-group{
  width: 100%;
}
.rowspan{
  display: inline-block;
  width: 150px;
}
.showtxt{
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 10px;
  height: 120px;
  overflow-y: auto;
}
.rowmb{
  margin-bottom: 10px;
}
</style>
